﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using QUANLYCHAMCONG.PL.CMS.Nhanvien;
using QUANLYCHAMCONG.BL.BUS;
using QUANLYCHAMCONG.BL.Entities;
using System.IO;
using Hopthoai;
using QUANLYCHAMCONG.PL.CMS;

namespace QUANLYCHAMCONG
{
    public partial class frmnhanvien : Form
    {
        int flag=0;
        string filename;
        STAFF nv;
        frmHoidap f;
        public delegate void _Reload();
        public event _Reload Reload;
        public frmnhanvien()
        {
            InitializeComponent();
        }
        public void GetadataAdd(int flag)
        {
            this.flag = flag;
        }
        public Total UcStatusRoom { get; set; }
        public void ReloadStattus()
        {
            Thongke tk = new Thongke();
            this.UcStatusRoom.DtData = BAOCAOBUS.ThongkeTotal(tk);
            this.UcStatusRoom.Settext();
        }
        public void Event()
        {
            if (Common.Flag == 1)
            {
                this.txtmanv.Text = CreateID();
                this.toolbar1.SetAdd_Edit_Delete();
                this.flag = Common.Flag;
                Common.Flag = 0;
                LoadData();
            }
            if (Common.Flag == 2)
            {
                try
                {
                    this.toolbar1.SetAdd_Edit_Delete();
                    this.flag = Common.Flag;
                    Common.Flag = 0;
                    LoadData();
                    nv = new STAFF();
                    nv.MANV=Common.MaNV;
                    Common.MaNV=null;
                    DataTable dt =new DataTable("NHANVIEN");
                    dt= STAFFBUS.SelectPK(nv);
                    if (dt.Rows.Count > 0)
                    {
                        DataRow dr = dt.Rows[0];
                        txtmanv.Text = dr["MANV"].ToString();
                        txthoten.Text = dr["TENNV"].ToString();
                        if (!String.IsNullOrEmpty(dr["ANH"].ToString()))
                        {
                            this.picnhanvien.Image = Image.FromFile(dr["ANH"].ToString());
                            this.filename = dr["ANH"].ToString();
                        }
                        else
                        {
                            this.picnhanvien.Image = global::QUANLYCHAMCONG.Properties.Resources.images;
                            this.filename = null;
                        }
                        txtdiachi.Text = dr["DIACHI"].ToString();
                        txtemail.Text=dr["EMAIL"].ToString();
                        txtghichu.Text = dr["GHICHU"].ToString();
                        if (dr["GIOITINH"].ToString() == "Nam")
                            rdnam.Checked = true;
                        else
                            rdnu.Checked = false;
                        if (this.cmbbc.FindString(dr["MABC"].ToString()) == -1)
                        {
                            cmbbc.SelectedIndex = 0;
                        }
                        else
                        {
                            cmbbc.SelectedIndex = this.cmbbc.FindString(dr["MABC"].ToString());
                        }
                        txtmacc.Text = dr["MACCM"].ToString();
                        if (this.cmbcty.FindString(dr["MACTY"].ToString()) == -1)
                        {
                            cmbcty.SelectedIndex = 0;
                        }
                        else
                        {
                            cmbcty.SelectedIndex = this.cmbcty.FindString(dr["MACTY"].ToString());
                        }
                        if (this.cmbcty.FindString(dr["MACV"].ToString()) == -1)
                        {
                            cmbcv.SelectedIndex = 0;
                        }
                        else
                        {
                           cmbcv.SelectedIndex = this.cmbcty.FindString(dr["MACV"].ToString());
                        }
                        if (this.cmbcty.FindString(dr["MANN"].ToString()) == -1)
                        {
                            cmbnn.SelectedIndex = 0;
                        }
                        else
                        {
                            cmbnn.SelectedIndex = this.cmbcty.FindString(dr["MANN"].ToString());
                        } 
                        txtmanv.Text=dr["MANV"].ToString();
                        if (this.cmbcv.FindString(dr["MAPB"].ToString()) == -1)
                        {
                            cmbpb.SelectedIndex = 0;
                        }
                        else
                        {
                            cmbpb.SelectedIndex = this.cmbcv.FindString(dr["MAPB"].ToString());
                        }
                       
                        dtpngaybdlv.Value = DateTime.Parse( dr["NGAYBD"].ToString());
                        dtpngaysinh.Value = DateTime.Parse(dr["NGAYSINH"].ToString());
                        dtpngayvaodang.Value=DateTime.Parse(dr["NGAYVAODANG"].ToString());
                        dtpngayvaodoan.Value=DateTime.Parse(dr["NGAYVAODOAN"].ToString());
                        txtnoisinhhoatdoan.Text=dr["NOISINHHOAT"].ToString();
                        txtnoisinhhoatdang.Text=dr["NOISINHHOATDANG"].ToString();
                        txtcmnd.Text = dr["SOCMND"].ToString();
                        txtdt.Text=dr["SODT"].ToString();
                        if (!String.IsNullOrEmpty(dr["TENCC"].ToString()))
                        {
                            txttencc.Text = dr["TENCC"].ToString();
                        }
                        else
                        {
                            txttencc.Text = "No Name";
                        }
                        txthoten.Text=dr["TENNV"].ToString();
                        txtmathedang.Text=dr["THEDANG"].ToString();
                        txtmathedoan.Text=dr["THEDOAN"].ToString();
                        if (dr["HONNHAN"].ToString() == "Doc than")
                            rddocthan.Checked = true;
                        else
                            rdcodg.Checked = true;
                        if (dr["UserEnabled"].ToString() == "WORK")
                            cbdanglam.Checked = true;
                        else
                            cbdanglam.Checked = false;
                        txtpass.Text = dr["UserPW"].ToString();
                        txtquyen.Text = dr["UserPrivilege"].ToString();
                        txtmathe.Text = dr["UserCardNo"].ToString();
                    }

                }
                catch(Exception ex) 
                {
                    f = new frmHoidap("Erorr :" + ex.Message.ToString(), frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                    f.ShowDialog();
                }
            }
            this.toolbar1.Close += new QUANLYCHAMCONG.PL.CMS.Toolbar._Close(toolbar1_Close);
            this.toolbar1.Save += new QUANLYCHAMCONG.PL.CMS.Toolbar._Save(toolbar1_Save);
            this.toolbar1.Add += new QUANLYCHAMCONG.PL.CMS.Toolbar._Add(toolbar1_Add);
        }

        void toolbar1_Add()
        {
            this.txtmanv.Text = CreateID();
            this.toolbar1.SetAdd_Edit_Delete();
            this.flag = 1;
            LoadData();

        }
        void LoadData()
        {
            try
            {
                //load commbo
                this.cmbcty.DataSource = CONGTYBUS.SelectAll();
                this.cmbcty.DisplayMember = "TENDKY";
                this.cmbcty.ValueMember = "MACTY";
                this.cmbcty.SelectedIndex = 0;
                this.cmbcv.DataSource = CHUCVUBUS.SelectAll();
                this.cmbcv.DisplayMember = "TENCV";
                this.cmbcv.ValueMember = "MACV";
                this.cmbcv.SelectedIndex = 0;
                this.cmbnn.DataSource = NGHENGHIEPBUS.SelectAll();
                this.cmbnn.DisplayMember = "TENNN";
                this.cmbnn.ValueMember = "MANN";
                this.cmbnn.SelectedIndex = 0;
                this.cmbpb.DataSource = PHONGBANBUS.SelectAll();
                this.cmbpb.DisplayMember = "TENPB";
                this.cmbpb.ValueMember = "MAPB";
                this.cmbpb.SelectedIndex = 0;
                this.cmbbc.DataSource = BANGCAPBUS.SelectAll();
                this.cmbbc.DisplayMember = "TENBC";
                this.cmbbc.ValueMember = "MABC";
                this.cmbbc.SelectedIndex = 0;
            }
            catch { }
        }
        void toolbar1_Save()
        {
            // su ly luu nhan vien
            nv=new STAFF();
            if (flag == 1)
            {
                if (STAFFBUS.Insert(AddData(nv)))
                {
                    f = new frmHoidap("Thêm nhân viên làm việc thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                    f.ShowDialog();
                    f = new frmHoidap("Bạn muốn tiếp tục thêm nhân viên ?", frmHoidap.CO.hien, frmHoidap.KHONG.hien, frmHoidap.THOAT.an, frmHoidap.HINH.dauhoi);
                    if (f.ShowDialog() == DialogResult.Yes)
                    {
                        this.toolbar1.SetSave();
                        Clear();
                        ReloadStattus();
                    }
                    else
                    {
                        this.Close();
                        if (this.Reload != null)
                        {
                            ReloadStattus();
                            this.Reload();
                        }
                    }
                }
            }
            if (flag == 2)
            { 
                if(STAFFBUS.Update(AddData(nv)))
                {
                    ReloadStattus();
                    f = new frmHoidap("Sửa thông tin nhân viên thành công!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.thongtin);
                    f.ShowDialog();
                    this.Close();
                    if (this.Reload != null)
                    {
                        ReloadStattus();
                        this.Reload();
                    }
                }
            }
        }
        private void Clear()
        {
            foreach (Control c in this.Controls)
            {
                if (c is TabControl)
                {

                    foreach (Control d in c.Controls)
                        if (d is TextBox)
                            d.Text = "";
                }
            }
            this.picnhanvien.Image = global::QUANLYCHAMCONG.Properties.Resources.images;
            this.txtmathe.Text = "0";
        }
        void toolbar1_Close()
        {
            this.Close();
        }
        private void frmnhanvien_Load(object sender, EventArgs e)
        {
            Event();
        }
        public string CreateID()
        {
            return STAFFBUS.GetID();
        }
        STAFF AddData(STAFF nv)
        {
            try
            {
                nv.ANH = filename;
                filename = null;
                nv.DIACHI = txtdiachi.Text;
                nv.EMAIL = txtemail.Text;
                nv.GHICHU = txtghichu.Text;
                if (rdnam.Checked == true)
                    nv.GIOITINH = true;
                else
                    nv.GIOITINH = false;
                nv.MABC = cmbbc.SelectedValue.ToString();
                nv.MACCM = txtmacc.Text;
                nv.MACTY = cmbcty.SelectedValue.ToString();
                nv.MACV = cmbcv.SelectedValue.ToString();
                nv.MANN = cmbnn.SelectedValue.ToString();
                nv.MANV = txtmanv.Text;
                nv.MAPB = cmbpb.SelectedValue.ToString();
                nv.NGAYBD = dtpngaybdlv.Value;
                nv.NGAYSINH = dtpngaysinh.Value;
                nv.NGAYVAODANG = dtpngayvaodang.Value;
                nv.NGAYVAODOAN = dtpngayvaodoan.Value;
                nv.NOISINHHOAT = txtnoisinhhoatdoan.Text;
                nv.NOISINHHOATDANG = txtnoisinhhoatdang.Text;
                nv.SOCMND = txtcmnd.Text;
                nv.SODT = txtdt.Text;
                nv.SONGAYPHEP = 0;
                nv.TENCC = txttencc.Text;
                nv.TENNV = txthoten.Text;
                nv.THEDANG = txtmathedang.Text;
                nv.THEDOAN = txtmathedoan.Text;
                if (rddocthan.Checked == true)
                    nv.TINHTRANGHONNHAN = false;
                else
                    nv.TINHTRANGHONNHAN = true;
                if (cbdanglam.Checked == true)
                    nv.UserEnabled = true;
                else
                    nv.UserEnabled = false;
                nv.UserCardNo = txtmathe.Text;
                if (!String.IsNullOrEmpty(txtquyen.Text))
                {
                    nv.UserPrivilege = int.Parse(txtquyen.Text);
                }
                else
                {
                    nv.UserPrivilege = 0;
                }
                nv.UserPW = txtpass.Text;

            }
            catch { }
            return nv;
        }
        private void picnhanvien_DoubleClick(object sender, EventArgs e)
        {
            if (this.flag == 2 || this.flag == 1)
            {
                try
                {
                    OpenFileDialog open = new OpenFileDialog();
                    open.Filter = "Image Files(*.BMP;*.JPG;*.JPEG;*.GIF)|*.BMP;*.JPG;*.JPEG;*.GIF|All files (*.*)|*.*";
                    open.Multiselect = false;
                    open.ShowDialog();
                    if (!String.IsNullOrEmpty(open.FileName))
                    {
                        string tenfile = open.SafeFileName;
                        filename = open.FileName;
                        string current = Application.StartupPath;
                        string chuoi =current + "\\App_Image\\Staff\\" + tenfile;
                        if (File.Exists(chuoi))
                        {
                            this.picnhanvien.Image = Image.FromFile(chuoi);
                        }
                        else
                        {
                            File.Copy(filename, chuoi);
                            this.picnhanvien.Image = Image.FromFile(chuoi);
                        }
                        
                        filename = chuoi;
                    }
                }
                catch
                {
                    return;
                }
            }
        }

        private void txtmacc_Leave(object sender, EventArgs e)
        {
            //kiem tra viec ton tai ma cham cong trong du lieu 
            nv=new STAFF();
            nv.MACCM=txtmacc.Text;
            if (STAFFBUS.ExistsNumBerMachine(nv)==false)
            {
                f = new frmHoidap("Mã chấm công này đã tồn tại vui lòng kiểm tra kỹ!",frmHoidap.CO.an,frmHoidap.KHONG.an,frmHoidap.THOAT.an,frmHoidap.HINH.baoloi);
                f.ShowDialog();
                txtmacc.Text = "";
                txtmacc.Focus();
            }
        }

        private void txttencc_Leave(object sender, EventArgs e)
        {
            nv = new STAFF();
            nv.TENCC = txttencc.Text;
            if (STAFFBUS.ExistsNameMachine(nv) == false)
            {
                f = new frmHoidap("Tên chấm công này đã tồn tại vui lòng kiểm tra kỹ!", frmHoidap.CO.an, frmHoidap.KHONG.an, frmHoidap.THOAT.an, frmHoidap.HINH.baoloi);
                f.ShowDialog();
                txttencc.Text = "";
                txttencc.Focus();
            }
        }
    }
}
